// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: '欢迎使用鹤龄π·颐养计划',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    canIUseNicknameComp: false,
    showFontPanel: false,
    selectedFontSize: 'normal',
    currentPage: 'index',
    showSidebar: false
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    if (wx.canIUse('button.open-type.getUserInfo')) {
      this.setData({
        canIUseNicknameComp: true
      })
    }
    const token = wx.getStorageSync('token')
    if (!token) {
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }
    
    // 获取全局实例
    const app = getApp()
    
    // 设置初始字体大小
    const currentFontSize = app.globalData.fontSize || 'normal'
    this.setData({
      selectedFontSize: currentFontSize
    })
  },
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }

    // 同步全局字体大小状态
    const app = getApp()
    this.setData({
      selectedFontSize: app.globalData.fontSize || 'normal'
    })
  },
  // 导航到不同页面
  navigateTo(e) {
    const page = e.currentTarget.dataset.page
    this.setData({
      currentPage: page
    })
    
    let url = ''
    switch(page) {
      case 'index':
        url = '/pages/index/index'
        break
      case 'medicine':
        url = '/pages/medicine/index'
        break
      case 'health':
        url = '/pages/health/index'
        break
      case 'emergency':
        url = '/pages/emergency/index'
        break
      case 'ai-chat':
        url = '/pages/ai-chat/index'
        break
      case 'data-export':
        url = '/pages/data-export/index'
        break
    }
    
    if (url) {
      wx.navigateTo({
        url: url
      })
    }
  },
  // 跳转到用药管理
  navigateToMedicine() {
    this.setData({
      currentPage: 'medicine'
    })
    wx.navigateTo({
      url: '/pages/medicine/index'
    })
  },
  // 跳转到健康监控
  navigateToHealth() {
    this.setData({
      currentPage: 'health'
    })
    wx.navigateTo({
      url: '/pages/health/index'
    })
  },
  // 跳转到紧急呼叫
  navigateToEmergency() {
    this.setData({
      currentPage: 'emergency'
    })
    wx.navigateTo({
      url: '/pages/emergency/index'
    })
  },
  // 显示/隐藏字体面板
  toggleFontPanel() {
    this.setData({
      showFontPanel: !this.data.showFontPanel
    })
  },
  // 更新字体大小
  onFontSizeChange(e) {
    const size = e.detail.value
    const app = getApp()
    
    // 更新本地状态
    this.setData({
      selectedFontSize: size,
      showFontPanel: false
    })

    // 调用全局方法更新字体大小
    app.setFontSize(size)

    // 显示提示
    wx.showToast({
      title: '已更新',
      icon: 'none',
      duration: 800
    })
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onToggleSidebar() {
    this.setData({ showSidebar: !this.data.showSidebar })
  },
  onSidebarNavigate(e) {
    const page = e.detail.page
    this.setData({
      currentPage: page,
      showSidebar: false
    })
    let url = ''
    switch(page) {
      case 'index': url = '/pages/index/index'; break
      case 'medicine': url = '/pages/medicine/index'; break
      case 'health': url = '/pages/health/index'; break
      case 'emergency': url = '/pages/emergency/index'; break
      case 'ai-chat': url = '/pages/ai-chat/index'; break
      case 'data-export': url = '/pages/data-export/index'; break
    }
    if (url) {
      wx.navigateTo({ url })
    }
  }
})
